package com.xkdx.guangguang;

import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.home.HomeFragment;
import com.xkdx.guangguang.fragment.home.HomeModule;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.presistence.uploaddevicetoken.UpLoadDeviceTokenAction;
import com.xkdx.guangguang.presistence.uploaddevicetoken.UpLoadDeviceTokenModule;
import com.xkdx.guangguang.presistence.uploaddevicetoken.UpLoadDeviceTokenPresistence;
import com.xkdx.guangguang.pushserviceforSelf.PushForSelfInfo;
import com.xkdx.guangguang.shareclass.BrandDetail;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class HomeActivity extends ManagerActivity {

	HomeFragment homeFragment;
	HomeModule homeModule;
	UpLoadDeviceTokenModule module;
	UpLoadDeviceTokenAction action;
	UpLoadDeviceTokenPresistence presistence;
	SharePrefenceUtil su;
	SharePrefenceUtil su_brand;
	public static List<BrandDetail> upDateBrandlist;
	PushForSelfInfo pushInfo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		su_brand = new SharePrefenceUtil(this, IConstants.SP_BRAND);
		su = new SharePrefenceUtil(this, "isUpLoadToken");
		action = new UpLoadDeviceTokenAction(this,
				su_brand.getBrandUpdateTime());
		module = new UpLoadDeviceTokenModule();
		presistence = new UpLoadDeviceTokenPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();
		IConstants.BottomBtnSelected = 3;
		if (homeFragment == null) {
			homeFragment = new HomeFragment();
		}
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.fragment, homeFragment, "homeFragment")
				.addToBackStack(null).commit();

		Bundle b = getIntent().getExtras();
		if (b != null) {

			pushInfo = (PushForSelfInfo) b.getSerializable("pushself");
			if (pushInfo != null && !("".equals(pushInfo.getObjectType()))) {
				IConstants.isHasSelfPush = pushInfo.isSelfNotificationState();
				//IConstants.isFromSelf = false;
				ProData(pushInfo);
			}
		} 

	}

	public HomeFragment getHomeFragment() {
		return homeFragment;
	}

	public void setHomeModule(HomeModule homeModule) {
		this.homeModule = homeModule;
	}

	public HomeModule getHomeModule() {
		return this.homeModule;
	}

	@Override
	protected void onStart() {
		IConstants.BottomBtnSelected = 3;
		super.onStart();
	}

	private void ProData(PushForSelfInfo pushInfo) {

		int type = Integer.valueOf(pushInfo.getObjectType());
		// String value = pushInfo.getObjectValue();
		if (type == 1) {
			//IConstants.isHasSelfPush = false;
		} else if (type == 2) {
			//IConstants.isHasSelfPush = false;
			IConstants.download(this, "dl.guangguang.net.cn/GuangGuang.apk");
		}
		// case 3:
		// HomeDetailFragment detail = new HomeDetailFragment();
		// Bundle b3 = new Bundle();
		// b3.putString("infoID", value);
		// detail.setArguments(b3);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, detail).addToBackStack(null)
		// .commitAllowingStateLoss();
		// break;
		// case 4:
		// ShopInfoFragment info = null;
		// if (info == null) {
		// info = new ShopInfoFragment();
		// }
		// Bundle b4 = new Bundle();
		// b4.putString("shopID", value);
		// info.setArguments(b4);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, info).addToBackStack(null)
		// .commitAllowingStateLoss();
		// break;
		// case 5:
		// BranchFragment branchFragment = new BranchFragment();
		// Bundle b5 = new Bundle();
		// b5.putString("shopID", value);
		// branchFragment.setArguments(b5);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, branchFragment)
		// .addToBackStack(null).commitAllowingStateLoss();
		// break;
		// case 6:
		// BrandInfoFragment brandInfo = new BrandInfoFragment();
		// Bundle b6 = new Bundle();
		// b6.putString("brandID", value);
		// brandInfo.setArguments(b6);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, brandInfo).addToBackStack(null)
		// .commitAllowingStateLoss();
		// break;
		// case 7:
		// NewsFragment newsFragment = new NewsFragment();
		// Bundle b7 = new Bundle();
		// b7.putString("infoID", value);
		// newsFragment.setArguments(b7);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, newsFragment).addToBackStack(null)
		// .commitAllowingStateLoss();
		//
		// break;
		// case 8:
		// DiscountFragment discountInfo = new DiscountFragment();
		// Bundle b8 = new Bundle();
		// b8.putString("infoID", value);
		// discountInfo.setArguments(b8);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.fragment, discountInfo).addToBackStack(null)
		// .commitAllowingStateLoss();
		//
		// break;
		else {
			IConstants.BottomBtnSelected = 5;
			Intent intent4 = new Intent(this, UserActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("pushself", pushInfo);
			IConstants.selfInfo = pushInfo;
			startActivity(intent4);
		}

	}

	@Override
	protected void onNewIntent(Intent intent) {

		super.onNewIntent(intent);
		setIntent(intent);
		// Toast.makeText(getApplicationContext(), "onnewintent", 0).show();

		if (intent != null) {
			Bundle b = getIntent().getExtras();
			if (b != null) {
				
				pushInfo = (PushForSelfInfo) b.getSerializable("pushself");
				if (pushInfo != null && !("".equals(pushInfo.getObjectType()))) {
					IConstants.isHasSelfPush = pushInfo.isSelfNotificationState();
					ProData(pushInfo);
				}
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (pushInfo == null) {
			if (IConstants.isHasPush) {
				int type = Integer.valueOf(IConstants.objectType);
				if (type == 1) {
					IConstants.isHasPush = false;
				} else if (type == 2) {
					IConstants.isHasPush = false;
					IConstants.download(this,
							"dl.guangguang.net.cn/GuangGuang.apk");
				} else {
					IConstants.BottomBtnSelected = 5;
					Intent intent4 = new Intent(this, UserActivity.class);
					startActivity(intent4);
				}

			}
		}
	}

	public void showLoaded(HashMap<String, AbsModule> map) {
		if (map.containsKey("0")) {
			if (module.isSucess) {
				su.setIsUpLoadToken(true);
			}
			su_brand.setBrandUpdateTime(module.updateTime);
			upDateBrandlist = module.list;
		}

	}

}
